<form class="layui-form seller-form"  action="" >
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">学号：</label>
            <div class="layui-input-inline" id="xh_box">
                <input type="text" id="xh" name="xh" class="layui-input" value="" placeholder="请输入学号" ></input>
            </div>
        </div> 
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="book_search"><i
                    class="iconfont icon-chaxun"></i>查询</button>      
            <button type="button" class="layui-btn layui-btn-sm book-return" id="book-return"><i class="layui-icon">&#xe705;</i> 退书</button>
        </div>
    </div>
</form>

<div class="table-body">
    <table id="bookTable" lay-filter="bookTable"></table>
</div>

<div id="returnDetail" style="display: none;">
    <form class="layui-form return-form"  action="" >    
        <div class="layui-form-item" style="margin: 20px;">
            <div class="layui-inline">
                <label class="layui-form-label"><i class="required-color">*</i> 退书原因：</label>
                <div class="layui-input-inline seller-inline-4">
                    <input type="text" name="draw_remark" required lay-verify="required" placeholder="请输入原因" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
    </form>
</div>

<script>
    layui.use(['table','form','layer','laydate'],function(){
        var layer = layui.layer, table = layui.table,form = layui.form,date = layui.laydate;

        var xh = '';
        //执行渲染
        table.render({
            elem: '#bookTable', //指定原始表格元素选择器（推荐id选择器）
            height: 'full-115',
            cellMinWidth: '80',
            page: 'true',
            limit:'20',
            id:'bookTable',
            url: "{:url('BookCommon/returnNull')}",
            cols: [[ //标题栏
                {type:'checkbox'},
                {field: 'xh', title: '学号', hide: true, width:200, align:'center'},
                {field: 'book_id', title: '书号', hide: true, width:200, align:'center'},
                {field: 'typeName', title: '类型', width:120, align:'center'},
                {field: 'plan_book_num', title: '教材数量', width:160, align:'center'},
                {field: 'plan_price', title: '单价', width:160, align:'center'},
                {field: 'book_disc', title: '折扣', width:160, align:'center'},
                {field: 'book_name', title: '教材名称', width:200, align:'center'},
                {field: 'book_author', title: '主编', width:160, align:'center'},
                {field: 'book_concern', title: '出版社', width:180, align:'center'},
                {field: 'book_edition', title: '版次', width:160, align:'center'},
                {field: 'book_isbn', title: 'ISBN', width:160, align:'center'},
            ]] //设置表头
            //,…… //更多参数参考右侧目录：基本参数选项
        });

        form.render();

        // 查询
        form.on('submit(book_search)', function (data) {
            table.reload('bookTable', {
                where: data.field,
                url: "{:url('BookReturn/getExtraBookList')}",
            });

            xh = data.field['xh'];
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
   
        //ajax提交退书
        $(document).on('click','.book-return',function(){
            var checkStatus = table.checkStatus('bookTable');
            data = checkStatus.data;
            if(data.length==0){
                layer.msg('请先选择教材！', { time: 1300 });
                return false;
            }

            let books = ""; 
            //let noPrice = false;
            $.each(data, function(index, item){ 
                // 如果没有单价则不允许调整
                // if(item.plan_price == '') {
                //     noPrice = true;
                //     return false; 
                // }

                books += item.type + ";" + item.xh + ";" + item.book_id + ";" + item.plan_book_num + ";" + item.plan_price + ";" + item.book_disc + ",";
            })

            // if(noPrice) {
            //     layer.msg('教材无单价信息，请先补充单价后再进行操作！', { time: 1300 });
            //     return false;
            // }

            books = books.substring(0, books.length - 1);

            layer.open({
                type: 1,
                title:'退书',
                area: ['380px', '200px'], //宽高
                btn:['确定','取消'],
                content: $("#returnDetail").html(),
                yes:function () {
                    if(!$(".layui-layer-btn0").hasClass("layui-btn-disabled")){
                        $(".layui-layer-btn0").addClass("layui-btn-disabled");

                        var formData = $(".return-form:last").serializeArray(); 
                        if($.trim(formData[0]['value']) == '') {
                            layer.msg('请填写退书原因！', { time: 1300 });
                            $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                            return false;
                        }

                        JsPost("{:url('BookReturn/bookReturn')}", {xh:xh, books:books, remark:formData[0]['value']},function(res){
                                layer.msg(res.msg, {time:1500}, function(){
                                    if(res.status){
                                        layer.closeAll();

                                        //刷新当前页面
                                        table.reload('bookTable', {
                                        });

                                        // 设置table请求参数（当操作的年度季度与刷新目标窗口不一致时）
                                        var where = {
                                            year : {$curr_year},
                                            season : {$curr_season}
                                        }

                                        // 设置筛选条件（当操作的年度季度与刷新目标窗口不一致时）
                                        var control = {
                                            '#year': {$curr_year},
                                            '#season': {$curr_season}
                                        }

                                        //执行跳转
                                        var topLayui = parent === self ? layui : top.layui;
                                        parent.layui.admin.events.changeTabAndRefresh('/manage/book_return/index.html', 'bookreturn', "{:url('BookReturn/index')}?year="+{$curr_year}+"&season="+{$curr_season}+"&dep_id=&specialty_id=&class_id=&xh=&draw_remark=", true, where, control);
                                    } else { 
                                        $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                    }
                                });
                            }
                        );
                    }
                },btn2:function () {
                    layer.closeAll();
                }, end: function(){
                }
            });
        });
 
    })
</script>